<?php

namespace App\Http\Controllers\Utils;

class ResponseFactory
{
    private static $BaseResponse = [
        'code' => 200,
        'msg' => "操作成功"
    ];


    /**
     * @param $data
     * @param $rows
     * @param int $code
     * @param string $msg
     * @return \Illuminate\Http\JsonResponse
     */
    public static function response($data = null, $rows = null, int $code = 200, string $msg = "操作成功"): \Illuminate\Http\JsonResponse
    {
        ResponseFactory::$BaseResponse["code"] = $code;
        ResponseFactory::$BaseResponse["msg"] = $msg;
        if ($data != null) {
            self::$BaseResponse["data"] = $data;
        }
        if ($rows != null) {
            self::$BaseResponse["rows"] = $rows;
        }
        return response()->json(self::$BaseResponse,$code);
    }

    /**
     * @param $data
     * @param int $code
     * @param string $msg
     * @return \Illuminate\Http\JsonResponse
     */
    public static function responseData($data, int $code = 200, string $msg = "操作成功"): \Illuminate\Http\JsonResponse
    {
        ResponseFactory::$BaseResponse["code"] = $code;
        ResponseFactory::$BaseResponse["msg"] = $msg;
        self::$BaseResponse["data"] = $data;
        return response()->json(self::$BaseResponse,$code);
    }

    /**
     * @param $rows
     * @param int $code
     * @param string $msg
     * @return \Illuminate\Http\JsonResponse
     */
    public static function responseRows($rows, int $code = 200, string $msg = "操作成功"): \Illuminate\Http\JsonResponse
    {
        ResponseFactory::$BaseResponse["code"] = $code;
        ResponseFactory::$BaseResponse["msg"] = $msg;
        self::$BaseResponse["rows"] = $rows;
        self::$BaseResponse["count"] = count($rows);
        return response()->json(self::$BaseResponse,$code);
    }


    /**
     * @param bool $state
     * @param string $msg
     * @return \Illuminate\Http\JsonResponse
     */
    public static function simpleResponse(bool $state, string $msg = "操作成功", int $code = 200): \Illuminate\Http\JsonResponse
    {
        if (!$state) {
            self::$BaseResponse['code'] = $code;
        }
        self::$BaseResponse['msg'] = $msg;
        return response()->json(self::$BaseResponse,$code);
    }

    /**
     * @param array $custom
     * @param int $code
     * @param string $msg
     * @return \Illuminate\Http\JsonResponse
     */
    public static function customResponse(array $custom = [], int $code = 200, string $msg = "操作成功"): \Illuminate\Http\JsonResponse
    {
        $response = [];
        $response["code"] = $code;
        $response["msg"] = $msg;
        return response()->json(array_merge($response, $custom),$code);
    }

}
